/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.dao.demouser;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.feyaSoft.home.dao.base.BaseHibernateDao;
import com.feyaSoft.home.dao.base.BaseSearchResult;
import com.feyaSoft.home.hibernate.demouser.Demouser;

/**
 * @author Fenqiang Zhuang
 * @Apr 25, 2007
 * 
 * This file is used to .....
 */
public class DemouserDaoImpl extends BaseHibernateDao implements DemouserDao {
	// log file
	private final static Logger log = Logger.getLogger(DemouserDaoImpl.class);
	private SessionFactory sessionFactory;

	public BaseSearchResult getDemouserList(
			DemouserSearchCriteria searchCriteria) throws Exception {
		Session session = sessionFactory.getCurrentSession();

		// start common search
		Criteria basicCriteria = buildBasicCriteria(searchCriteria, session);

		BaseSearchResult result = null;
		try {
			result = commonSearch(session, basicCriteria, searchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("exception during call DemouserDaoImpl");
		}

		return result;
	}

	/**
	 * pass the criteria and searchCriteria to build. this part only involve in
	 * main part, without first, max value
	 * 
	 * @param pc
	 * @param basicQuery
	 * @return criteria
	 */
	private Criteria buildBasicCriteria(DemouserSearchCriteria demouserSc,
			Session session) {
		Criteria basicCriteria = session.createCriteria(Demouser.class);

		// order property...
		if (demouserSc.getOrderProperty() != null
				&& demouserSc.getOrderProperty().length() > 0) {
			basicCriteria.addOrder(Order.desc(demouserSc.getOrderProperty()));
		}

		// dealing with USERNAME condition
		if (demouserSc.getUsernames() != null
				&& demouserSc.getUsernames().size() > 0) {
			Disjunction disjunction = buildDisjunction(demouserSc
					.getUsernames(), SQL_LIKE, "username");
			basicCriteria.add(disjunction);
		}

		// dealing with name condition
		if (demouserSc.getNames() != null && demouserSc.getNames().size() > 0) {
			String[] alias = { "firstName", "lastName" };
			Disjunction disjunction = buildDisjunction(demouserSc.getNames(),
					SQL_LIKE, alias);
			basicCriteria.add(disjunction);
		}

		return basicCriteria;
	}

	/**
	 * load Demouser by id now
	 */
	public Demouser loadDemouserById(Long id) throws Exception {
		Demouser demouser = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			demouser = (Demouser) session.get(Demouser.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			log.fatal("loadDemouserById failed" + e.getMessage());
		}
		return demouser;
	}

	/**
	 * Load Demouser by username
	 */
	public Demouser loadDemouserByUsername(String username) throws Exception {
		Demouser demouser = null;

		try {
			Session session = sessionFactory.getCurrentSession();
			List<Demouser> accounts = session.createCriteria(Demouser.class)
					.add(Restrictions.eq("username", username)).list();

			if (accounts != null && accounts.size() == 1) {
				demouser = (Demouser) accounts.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.fatal("loadDemouserById failed" + e.getMessage());
		}

		return demouser;
	}

	/**
	 * save or update demouser
	 */
	public Long saveOrUpdate(Demouser demouser) throws Exception {
		Long id = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			session.saveOrUpdate(demouser);
			id = demouser.getId();
		} catch (Exception e) {
			e.printStackTrace();
			log.fatal("saveOrUpdate demouser failed, " + e.getMessage());
		}

		return id;
	}
	
	/**
	 * delete demouser
	 */
	public void delete(Demouser demouser) throws Exception {
		try {
			Session session = sessionFactory.getCurrentSession();
			session.delete(demouser);
		} catch (Exception e) {
			e.printStackTrace();
			log.fatal("delete demouser failed, " + e.getMessage());
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
